package com.mxfcode.bbs_common.table_operation.controller;


import com.mxfcode.bbs_common.table_operation.service.UtilService;
import com.mxfcode.bbs_common.util.AjaxJson;
import com.mxfcode.bbs_common.util.CommUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * @类名 DefaultFormController
 * @描述 TODO
 * @作者 mxf
 * @创建日期 2020/11/9 19:35
 **/
@Controller
public class DefaultFormController {
    @Autowired
    UtilService utilService;

    @ResponseBody
    @RequestMapping(params = "deleteByIds")
    public AjaxJson deleteByIds(HttpServletRequest request, String tableName, String ids){
        AjaxJson json=new AjaxJson();
        Integer isTrue = utilService.deleteDataForIds(tableName,ids);
        if (isTrue > 0){
            json.setSuccess(true);
            json.setMsg("删除成功");
            return json;
        }else {
            json.setSuccess(false);
            json.setMsg("删除失败");
            return json;
        }
    }

    /**
     * 更新方法
     * @param
     * @return
     */
    @RequestMapping(params = "dataUpdata")
    @ResponseBody
    public AjaxJson updataSave(HttpServletRequest request){
        AjaxJson json = new AjaxJson();
        //得到参数
        Map dataMap =request.getParameterMap();
        Map data = CommUtils.mapConvert(dataMap);
        String tableName=data.get("tableName").toString();
        //去掉多余的参数
        data.remove("updateSave");
        data.remove("tableName");
        boolean isTrue = utilService.updateDataMap(tableName,data);
        if (isTrue == true){
            json.setSuccess(true);
            json.setMsg("更新成功");
            return json;
        }else {
            json.setSuccess(false);
            json.setMsg("更新失败");
            return json;
        }
    }

    /**
     * 保存方法
     * @param
     * @return
     */
    @RequestMapping(params = "dataSave")
    @ResponseBody
    public AjaxJson dataSave(HttpServletRequest request){
        AjaxJson json = new AjaxJson();
        //得到参数
        Map dataMap =request.getParameterMap();
        Map data = CommUtils.mapConvert(dataMap);
        String tableName=data.get("tableName").toString();
        //去掉多余的参数
        data.remove("dataSave");
        data.remove("tableName");
        Integer rows = utilService.insertOneData(tableName, data);
        if (rows > 0){
            json.setSuccess(true);
            json.setMsg("保存成功");
            return json;
        }else {
            json.setSuccess(false);
            json.setMsg("保存失败");
            return json;
        }
    }
}
